// 新增管理员
function addAdminFun() {
    $("#addUserBtn").on("click", function () {
        var userName = $(".addUserName").val();
        var userPassword = $(".addUserPassword").val();
        var checkUserPassword = $(".checkAddUserPassword").val();
        var userLevelId = $(".addUserLevelId:checked").val();
        var userState = $(".addUserState:checked").val();
        if (userName != null && userName.length != 0
            && userPassword != null && userPassword.length != 0
            && checkUserPassword != null && checkUserPassword.length != 0) {
            if (userPassword != null && userPassword.length != 0
                && checkUserPassword != null && checkUserPassword.length != 0) {
                if (userPassword == checkUserPassword) {
                    $.ajax({
                        type: "POST",
                        url: "/adminManage/checkUserName",
                        dataType: "json",
                        data: {
                            userName: userName
                        },
                        success: function (data) {
                            if (data.result == null) {
                                $.ajax({
                                    type: "POST",
                                    url: "/adminManage/addAdmin",
                                    dataType: "json",
                                    data: {
                                        userName: userName,
                                        userPassword: userPassword,
                                        userLevelId: userLevelId,
                                        userState: userState
                                    },
                                    success: function (data) {
                                        if (data.result == "success") {
                                            alert("新增成功");
                                            window.location.reload();
                                        } else {
                                            alert("新增失败");
                                        }
                                    },
                                    error: function () {
                                        alert("服务器错误，操作失败");
                                    }
                                });
                            } else {
                                alert("该用户名已经被使用，请重新输入");
                            }
                        },
                        error: function () {
                            alert("服务器错误，操作失败");
                        }
                    });
                } else {
                    alert("两次输入的密码不一致，请重新输入");
                }
            } else {
                alert("请输入新密码与确认密码");
            }
        } else {
            alert("请输入或设置所有信息，不能留有空白");
        }
    });
}

// 删除管理员
function deleteAdminFun(userId) {
    $("#deleteAdminBtn").on("click", function () {
        $.ajax({
            type: "POST",
            url: "/adminManage/deleteAdmin",
            dataType: "json",
            data: {
                userId: userId
            },
            success: function (data) {
                if (data.result == "success") {
                    alert("删除成功");
                    window.location.reload();
                } else {
                    alert("删除失败");
                }
            },
            error: function () {
                alert("服务器错误，操作失败");
            }
        });
    });
}

// 重置密码
function updateAdminPasswordFun(userId, userName) {
    $("#updateAdminPasswordBtn").on("click", function () {
        var userPassword = $(".userPasswordReset").val();
        var checkUserPassword = $(".checkUserPasswordReset").val();
        if (userPassword != null && userPassword.length != 0
            && checkUserPassword != null && checkUserPassword.length != 0) {
            if (userPassword == checkUserPassword) {
                $.ajax({
                    type: "POST",
                    url: "/adminManage/updateAdmin",
                    dataType: "json",
                    data: {
                        userId: userId,
                        userName: userName,
                        userPassword: userPassword
                    },
                    success: function (data) {
                        if (data.result == "success") {
                            alert("更新成功");
                            window.location.reload();
                        } else {
                            alert("更新失败");
                        }
                    },
                    error: function () {
                        alert("服务器错误，操作失败");
                    }
                });
            } else {
                alert("两次输入的密码不一致，请重新输入");
            }
        } else {
            alert("请输入新密码与确认密码");
        }
    });
}

// 查看 更新
function adminFun(userId) {
    $.ajax({
        type: "GET",
        url: "/adminManage/getAdmin?userId=" + userId,
        dataType: "json",
        success: function (data) {
            var user = data.user;
            if (user != null) {
                $(".userId").val(user.userId);
                $(".userName").val(user.userName);
                switch (user.userLevelId) {
                    case 1 :$(".userLevelId").eq(0).attr("checked", "true"); break;
                    case 2 :$(".userLevelId").eq(1).attr("checked", "true"); break;
                    default : alert("设置等级异常");
                }
                switch (user.userState) {
                    case 0 :$(".userState").eq(0).attr("checked", "true"); break;
                    case 1 :$(".userState").eq(1).attr("checked", "true"); break;
                    default : alert("设置等级异常");
                }
            } else {
                alert("未查询到数据 bug");
            }
        },
        error: function () {
            alert("服务器错误，操作失败");
        }
    });
    // 更新
    $("#updateAdminBtn").on("click", function () {
        var userId = $(".userId").val();
        var userLevelId = $(".userLevelId:checked").val();
        var userState = $(".userState:checked").val();
        if (userLevelId != null && userLevelId.length != 0
            && userState != null && userState.length != 0) {
            $.ajax({
                type: "POST",
                url: "/adminManage/updateAdmin",
                dataType: "json",
                data: {
                    userId: userId,
                    userLevelId: userLevelId,
                    userState: userState
                },
                success: function (data) {
                    if (data.result == "success") {
                        alert("更新成功");
                        window.location.reload();
                    } else {
                        alert("更新失败");
                    }
                },
                error: function () {
                    alert("服务器错误，操作失败");
                }
            });
        } else {
            alert("请输入或设置所有信息，不能留有空白");
        }
    });
}

// 登录
$(".loginFormBtn").on("click", function () {
    var userName = $(".userName").val();
    var userPassword = $(".userPassword").val();
    if (userName.length == 0 || userName == null || userPassword.length == 0 || userPassword == null) {
        $(".login-message").html("用户名或者密码不能为空").css({"color": "red"});
    } else {
        $.ajax({
            type: "POST",
            url: "/login",
            data: $("#loginForm").serialize(),
            dataType: "json",
            success: function(data) {
                if (data.message == "success") {
                    if (data.url != null) {
                        window.location.href=data.url;
                    } else {
                        window.location.href="/index";
                    }
                } else {
                    $(".login-message").html(data.message).css({"color": "red"});
                }
            },
            error: function () {
                $(".login-message").html("ajax 服务器错误").css({"color": "red"});
            }
        });
    }
});